package com.winsdom.controller;



import com.winsdom.anno.Authentication;
import com.winsdom.annotation.OperationLog;
import com.winsdom.enums.AuthenticationType;
import com.winsdom.result.Result;
import com.winsdom.service.ScheduledNotificationService;
import com.winsdom.service.SmsTemplateService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;


import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/sms/templates")
@Api(tags = "通知模板", description = "通知发送相关接口")
@Authentication(value = {AuthenticationType.NOTIFICATION_TEMPLATE})
public class SmsController {


    @Autowired
    private SmsTemplateService smsTemplateService;

    @Autowired
    private ScheduledNotificationService scheduledNotificationService;



    /**
     * 查询所有模板
     * @return
     */
    @Operation(summary = "展示全部模板")
    @OperationLog(description = "发送全部模板")
    @GetMapping("/all")
    public Result<List<Map<String, Object>>> getSmsTemplates() {
        try {
            List<Map<String, Object>> result = smsTemplateService.getAllSmsTemplates();
            Result<List<Map<String, Object>>> res = Result.success(result);
            res.setMsg("查询成功");
            return res;
        } catch (Exception e) {
            return Result.error(500, "系统异常：" + e.getMessage());
        }
    }

    /**
     * 发送电子发票通知（通过客户ID）
     * @param customerId
     * @return
     */
    @Operation(summary = "发送电子发票通知")
    @OperationLog(description = "发送电子发票通知")
    @GetMapping("/invoicing")
    public Result<String> sendInvoicingNotification(@PathVariable("customerId") @NotNull(message = "客户ID不能为空") Long customerId) {
        try {
            smsTemplateService.sendInvoicingNotification(customerId);
            return Result.success("电子发票通知发送成功");
        } catch (Exception e) {
            return Result.error(500, "系统异常：" + e.getMessage());
        }
    }

    /**
     * 发送电子收据通知（通过客户ID）
     * @param customerId
     * @return
     */
    @Operation(summary = "发送电子收据通知")
    @OperationLog(description = "发送电子收据通知")
    @GetMapping("/issurReceipt")
    public Result<String> sendIssurReceiptNotification(@PathVariable("customerId") @NotNull(message = "客户ID不能为空") Long customerId) {
        try {
            smsTemplateService.sendIssurReceiptNotification(customerId);
            return Result.success("电子收据通知发送成功");
        } catch (Exception e) {
            return Result.error(500, "系统异常：" + e.getMessage());
        }
    }





    /**
     * 批量发送账单通知（通过账单编号）
     * @param billNos 账单编号
     * @return 发送结果
     */
    @Operation(summary = "批量发送账单通知（通过账单编号列表）")
    @OperationLog(description = "批量发送账单通知")
    @GetMapping("/billNotification/multiple")
    public Result<Map<String, Object>> sendBillNotificationMultiple(
            @RequestParam @NotNull(message = "账单编号列表不能为空") List<String> billNos
    ) {
        try {
            Map<String, Object> result = smsTemplateService.sendBillNotificationMultiple(billNos);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(500, "系统异常：" + e.getMessage());
        }
    }





    /**
     * 批量发送催收通知（支持多个客户ID）
     * @param customerIds 客户ID列表
     * @return 操作结果（包含成功/失败的客户ID）
     */
    @Operation(summary = "批量发送催收通知（通过客户ID列表）")
    @OperationLog(description = "批量发送催收通知")
    @GetMapping("/collectReminder/multiple")
    public Result<Map<String, Object>> sendCollectReminderMultiple(
            @RequestParam @NotNull(message = "客户ID列表不能为空") List<Long> customerIds
    ) {
        try {
            Map<String, Object> result = smsTemplateService.sendCollectReminderMultiple(customerIds);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(500, "系统异常：" + e.getMessage());
        }
    }




    /*-------------------定时任务-----------------------------------*/


    /**
     * 手动触发催缴通知（测试用）
     * @return
     */
    @Operation(summary = "手动触发催缴通知（测试用）")
    @OperationLog(description = "手动触发催缴通知")
    @GetMapping("/scheduled/sendPayment")
    public Result<String> triggerPaymentReminder() {
        try {
            scheduledNotificationService.scheduledSendPaymentReminder();
            return Result.success("催收通知任务手动触发成功");
        }catch (Exception e) {
            return Result.error(500, "系统异常：" + e.getMessage());
        }
    }

}

